package com.yiqi.liebang.common.rongim.msg;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.db.UserOpe;

import cn.rongcloud.contactcard.Friend;
import io.rong.imkit.model.ConversationProviderTag;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.utils.RongDateUtils;
import io.rong.imkit.widget.provider.PrivateConversationProvider;


@ConversationProviderTag(conversationType = "private", portraitPosition = 1)
public class MyPrivateConversationProvider extends PrivateConversationProvider {

    private Context context;

    class ViewHolder extends PrivateConversationProvider.ViewHolder {

        TextView mTvUserMsgName;
        ImageView mIvMsgCertification;
        TextView mTvUserMsgPosition;
        ImageView mIvUserPosition;
        TextView mIvUserMsgTime;
        TextView mTvUserMsgContent;
    }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.item_rc_list, null);
        MyPrivateConversationProvider.ViewHolder holder = new MyPrivateConversationProvider.ViewHolder();
//        holder.mIvUserMsgHead = view.findViewById(R.id.iv_user_msg_head);


        holder.mTvUserMsgName = view.findViewById(R.id.tv_user_msg_name);



        holder.mIvMsgCertification = view.findViewById(R.id.iv_msg_certification);
        holder.mTvUserMsgPosition = view.findViewById(R.id.tv_user_msg_position);
        holder.mIvUserPosition = view.findViewById(R.id.iv_user_position);
        holder.mIvUserMsgTime = view.findViewById(R.id.iv_user_msg_time);
        holder.mTvUserMsgContent = view.findViewById(R.id.tv_user_msg_content);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View view, int position, UIConversation data) {
//        T.showShort(data.sen);

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.mTvUserMsgName.setText(data.getUIConversationTitle());
        holder.mTvUserMsgContent.setText(data.getConversationContent());
        holder.mIvUserMsgTime.setText(RongDateUtils.getConversationListFormatDate(data.getUIConversationTime(), view.getContext()));

        try {
            Friend friend = UserOpe.queryById(context, data.getConversationTargetId());
            if (friend!=null){
                holder.mIvMsgCertification.setVisibility(friend.getIsBasic() == 1 ? View.VISIBLE : View.GONE);
                holder.mIvUserPosition.setVisibility(friend.getIsOccupation() == 1 ? View.VISIBLE : View.GONE);
                holder.mTvUserMsgPosition.setText(TextUtils.isEmpty(friend.getPosition())?"":" · "+friend.getPosition());
            }
        } catch (Exception e) {
            Logger.e("Exception----->" + e.getMessage());
        }


    }


}